package com.nthink.indicators.service.operationindex.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nthink.indicators.mapper.operationindex.AssetMapper;
import com.nthink.indicators.pojo.hospitalindicators.HospitalIncome;
import com.nthink.indicators.pojo.operationindex.assetoperation.AssetData;
import com.nthink.indicators.service.operationindex.AssetService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AssetServiceImpl extends ServiceImpl<AssetMapper, AssetData> implements AssetService {


    @Override
    public AssetData getAssetData(Date startTime, Date endTime) {
        AssetData assetData = new AssetData();

        double inIncome = 0;
        double outIncome = 0;

        double inDrugIncome = 0;
        double outDrugIncome = 0;

        double inMaterialIncome = 0;
        double outMaterialIncome = 0;

        //非医疗服务收入（药品、耗材、检查检验收入）
        double noTreatmentIncome = 0;

        double inMedicalInsuranceIncome = baseMapper.getInMedicalInsuranceIncome(startTime, endTime);
        double outMedicalInsuranceIncome = baseMapper.getOutMedicalInsuranceIncome(startTime, endTime);

        List<HospitalIncome> hospitalIncome = baseMapper.getHospitalIncome(startTime, endTime);

        for (HospitalIncome income : hospitalIncome) {
            switch (income.getCateID()) {
                case "020416"://西药
                case "020417"://中草药
                case "020404"://中成药
                    inDrugIncome += income.getInAmount();
                    outDrugIncome += income.getOutAmount();

                    noTreatmentIncome += income.getOutAmount();
                    noTreatmentIncome += income.getInAmount();
                    break;
                case "020412"://卫生材料费
                    inMaterialIncome += income.getInAmount();
                    outMaterialIncome += income.getOutAmount();

                    noTreatmentIncome += income.getOutAmount();
                    noTreatmentIncome += income.getInAmount();
                    break;
                case "020406"://检查费
                case "020407"://化验费
                    noTreatmentIncome += income.getOutAmount();
                    noTreatmentIncome += income.getInAmount();
                    break;
            }

            inIncome += income.getInAmount();
            outIncome += income.getOutAmount();
        }

        double totalIncome = inIncome + outIncome;
        double totalDrugIncome = inDrugIncome + outDrugIncome;
        double totalMaterialIncome = inMaterialIncome + outMaterialIncome;

        //医疗服务收入（不含药品、耗材、检查检验收入）
        double TreatmentIncome = totalIncome - noTreatmentIncome;

        //医疗收入中药品收入比率

        assetData.setDrugRate(totalDrugIncome == 0 ? 0 : totalDrugIncome / totalIncome);
        //医疗收入中医用材料收入比率
        assetData.setMaterialRate(totalMaterialIncome == 0 ? 0 : totalMaterialIncome / totalIncome);
        //门诊收入占医疗收入比例
        assetData.setOutRate(outIncome == 0 ? 0 : outIncome / totalIncome);
        //门诊收入中来自医保基金的比例
        assetData.setOutMedicalInsuranceRate(outMedicalInsuranceIncome == 0 ? 0 : outMedicalInsuranceIncome / outIncome);
        //住院收入占医疗收入比例
        assetData.setInRate(inIncome == 0 ? 0 : inIncome / totalIncome);
        //住院收入中来自医保基金的比例
        assetData.setInMedicalInsuranceRate(inMedicalInsuranceIncome == 0 ? 0 : inMedicalInsuranceIncome / inIncome);
        //医疗服务收入（不含药品、耗材、检查检验收入）
        assetData.setTreatmentIncome(TreatmentIncome);

        return assetData;
    }


}
